﻿#region Licence
// Copyright (c) 2011 BAX Services Bartosz Adamczewski
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSpec.Shell.Display;
using CSpec.Testing;

namespace CSpec.Shell.Execution
{
    /// <summary>
    /// Base Action that servers as an entry point for all other actions.
    /// If possible all other actions should <b>always</b> inherit from this
    /// action.
    ///</summary>
    public class BaseAction
    {
        /// <summary>
        /// Protected Console class.
        /// </summary>
        protected CSpecConsole Console {get; private set;}
        /// <summary>
        /// Protected Console formatter, in case specific action
        /// needs a different color and line set.
        /// </summary>
        protected ConsoleFormatter Formatter { get; private set; }

        /// <summary>
        /// Default constructor that initializes the console and it's formatters.
        /// </summary>
        public BaseAction()
        {
            Formatter = new ConsoleFormatter();
            Console = new CSpecConsole(Formatter);
        }

        /// <summary>
        /// Helper method to display a well formatted help information,
        /// all Actions should call this when implementing the method 'DisplayInterface'
        /// as this enshures that all Actions will have the same formatting when displaying help
        /// screens.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="description"></param>
        /// <param name="commands"></param>
        /// <param name="usesFiles"></param>
        protected void DisplayHelp(string key, string description, List<ICommand> commands, bool usesFiles)
        {
            string help = string.Empty;

            if (usesFiles)
                help += " [FILES]";

            if (commands != null && commands.Count != 0)
            {
                help += " [OPTIONS] ";
            }

            Console.WriteInfo(key + help + " : " + description);
            foreach (var cmd in commands)
                Console.WriteInfo(cmd.Key + " : " + cmd.Description);
        }
    }
}
